<?php
	class estatus extends Controller {
		public function __construct() {
			parent::Controller();
			$this->load->model('mestatus');
			$this->load->helper(array('form','url'));
			$this->load->library(array('upload','form_validation'));
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		}

		function index() {
			$this->load->view('main', array('content_page' => 'estatus_list','menutop' => 0));
		}

		function add() {
			$this->load->view('main', array('content_page' => 'estatus_add','menutop' => 0));
		}

		function edit($id_estatus) {
			$estatus = $this->mestatus->LoadEstatusById($id_estatus);
			$this->load->view('main', array('content_page' => 'estatus_edit','menutop' => 0, 'estatus' => $estatus));
		}

		function delete() 
	{
			$ids = $this->input->post('delete_rows');
			if(strpos($ids,"|") === false) 
			{ 
				if($this->mestatus->Delete($ids) !== false) 
				{
					echo 'El Registro fue eliminado satisfactoriamente';
				} 
				else 
				{
						echo 'Hubo un error eliminando el Registro.  Intentelo nuevamente mas tarde 						o comuniquese con el administrador';
				}
				
			} 
			else 
			{
				$allGood = true;$allRelationGood = true;
				while(strlen($ids) > 0) 
				{
					if(strpos($ids, "|") !== false) {
						$actual = substr($ids, 0,strpos($ids, "|"));
						$ids = substr($ids,strpos($ids, "|") + 1);
					} else {
						$actual = $ids;
						$ids = "";
					}

				
						if($this->mestatus->Delete($actual) == 0) 
						{//Error deleting
							$allGood = false;
						}
					}
					echo 'Los Registros fueron eliminados satisfactoriamente';
				}
	}
	
		function save() {
			$this->form_validation->set_rules('name', '"Nombre"', 'trim|required');

			if ($this->form_validation->run()) {
				$this->session->set_flashdata('status_title', 'Mantenimiento de Estatus');
				$this->session->set_flashdata('status_url', 'process_estatus');

				if($this->input->post('id_estatus') === false) {
					if($this->mestatus->CreateEstatus($this->input->post('name'), $this->input->post('desc'), $this->input->post('dias')) != false)
						$this->session->set_flashdata('status_message', 'El registro ha sido agregado satisfactoriamente');
					else
						$this->session->set_flashdata('status_message', 'Ha ocurrido un error agregando registro. Intentelo nuevamente mas tarde o comuniquese con el administrador');
				} else {
					$estatus = $this->mestatus->LoadEstatusById($this->input->post('id_estatus'));
					if($estatus != false) {
						$estatus->setNombre($this->input->post('name'));
						$estatus->setDesc($this->input->post('desc'));
						$estatus->setDias($this->input->post('dias'));

						if($this->mestatus->UpdateEstatus($estatus) != false)
							$this->session->set_flashdata('status_message', 'El registro ha sido actualizado satisfactoriamente');
						else
							$this->session->set_flashdata('status_message', 'Ha ocurrido un error actualizando el registro. Intentelo nuevamente mas tarde o comuniquese con el administrador');
					} else
						$this->session->set_flashdata('status_message', 'Ha ocurrido un error actualizando el registro. Intentelo nuevamente mas tarde o comuniquese con el administrador');
				}
				redirect('estatus/estatus');
			}

			$view = 'estatus_add';
			$estatus = false;
			if($this->input->post('id_estatus') !== false) {
				$view = 'estatus_edit';
				$estatus = $this->estatus->LoadEstatusById($this->input->post('id_estatus'));
			}

			$this->load->view('main', array('content_page' => $view, 'estatus' => $estatus));
		}

		function process_status() {
			$this->load->view('main', array('content_page' => 'status'));
		}

		function data() {
			$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
			$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'ID_airport'; // Columna para el order by
			$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
			$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
			$start = $limit * $page - $limit;
			if($start < 0) $start = 0;

			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );

			$records = $this->mestatus->LoadEstatus($this->input->post('name'), $this->input->post('desc'), $start, $limit, $sortname, $sortorder);

			$totalRecords = $this->mestatus->GetCount($this->input->post('name'), $this->input->post('desc'));

			$totalPages = ceil($totalRecords/$limit);
			if ($page > $totalPages) $page = $totalPages;

			$json_output = array(
				'page'		=> $page,
				'total'		=> $totalPages,
				'records'	=> $totalRecords,
				'rows'		=> array()
			);

			if($records != false) {
				foreach($records as $estatus) {
					$arr = $json_output['rows'][] = array(
					     'IdEstatus' => $estatus->getIdEstatus(),
					 	 'NombreStatus' => $estatus->getNombre(), 
					     'DescStatus' => $estatus->getDesc(),
					     'dias' => $estatus->getDias()
					);
				}
			}

			echo json_encode($json_output);
		}
	}
?>